//
//  LZFTextField.m
//  LZFBuDieJie
//
//  Created by 梁梓烽 on 16/6/22.
//  Copyright © 2016年 liangzifeng. All rights reserved.
//

#import "LZFTextField.h"
#import "UITextField+Placeholder.h"
// 默认的占位文字颜色
#define LZFPlaceholderDefaultColor [UIColor grayColor]
// 聚焦的占位文字颜色
#define LZFPlaceholderFocusColor [UIColor whiteColor]


/*
 1.光标颜色为白色
 2.textfield开始编辑的时候,占位文字变为白色
 3.textfield结束编辑的时候,占位文字变为银白色
 
 
 */
@implementation LZFTextField

 -(void)awakeFromNib
{
    //设置光标颜色
    self.tintColor=LZFPlaceholderFocusColor;
    
    //设置文字颜色
    self.textColor=LZFPlaceholderFocusColor;
    
    //监听文本框开始编辑
    [self addTarget:self action:@selector(startEdit) forControlEvents:UIControlEventEditingDidBegin];
    
    
    
    //监听文本框结束编辑
     [self addTarget:self action:@selector(endEdit) forControlEvents:UIControlEventEditingDidEnd];
    
    //UITextField *tf;
   
}



/*
 方法一: 通过设置textfield的attriburePlacehoder属性来改变占位文字的颜色
-(void)startEdit
{
    NSMutableDictionary *dic=[NSMutableDictionary new];
    dic[NSForegroundColorAttributeName]=LZFPlaceholderFocusColor;
    self.attributedPlaceholder=[[NSAttributedString alloc]initWithString:self.placeholder attributes:dic];
}

-(void)endEdit
{
    
    NSMutableDictionary *dic=[NSMutableDictionary new];
    dic[NSForegroundColorAttributeName]=LZFPlaceholderDefaultColor;
    self.attributedPlaceholder=[[NSAttributedString alloc]initWithString:self.placeholder attributes:dic];
}
*/

/*
 方法二:通过打断点查看到textField里面的一个私有属性_placeholderLabel,通过kvc拿到该私有指针指向的对象,然后再修改对象的属性
-(void)startEdit
{
    
    UILabel *placeHolderLabel = [self valueForKey:@"_placeholderLabel"];
    placeHolderLabel.textColor=LZFPlaceholderFocusColor;
    

}

-(void)endEdit
{
    UILabel *placeHolderLabel = [self valueForKey:@"_placeholderLabel"];
    placeHolderLabel.textColor=LZFPlaceholderDefaultColor;
 }
*/


//方法三: 使用runtime来交换方法  导入分类

-(void)startEdit
{
    
    self.placeholderColor=LZFPlaceholderFocusColor;
    
}

-(void)endEdit
{
    self.placeholderColor=LZFPlaceholderDefaultColor;

}

@end
